const app = getApp();
Page({
  data: {
    is_login:false,
    year:'',
    month:'',
    list:[],
    rece_sum:0.00,
    pay_sum:0.00,
    index_bg_img:wx.getStorageSync('index_bg_img'),
  },

  onShow:function(){
    this.getUserInfo(); //获取用户信息
  },

  onLoad:function(e){
    let scene = decodeURIComponent(e.scene);
    let arr1 = scene.split('&');
    let invite = '';
    for (var i = 0; i < arr1.length; i++) {
        let key = arr1[i].split("=")[0];
        if (key == 'inviteCode'){
            invite = arr1[i].split("=")[1];
        }
    }
    if(invite != ''){
      //设置全局邀请参数
      wx.setStorageSync("inviteCode",invite);
    }
    this.init_date(); //初始化时间
  },

  //初始化时间
  init_date:function(){
    let _this = this;
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    if (month >= 1 && month <= 9) {
      month = "0" + month;
    }
    _this.setData({
      year: year,
      month: month
    })
  },

  //获取个人信息
  getUserInfo:function(){
    let _this = this;
    app._post('api/member/getUserInfo', {}, function(result) {
      if(result.code == 200){
        _this.setData({
          is_login:true,
        })
        //判断是否有设置自定义皮肤
        if(result.data.index_bg != 0){
          _this.get_pifu(result.data.index_bg);
        }
        _this.get_bill_list();
      }else{
        _this.setData({
          is_login:false,
        })
      }
    });
  },

  //获取皮肤
  get_pifu:function(index_bg){
    let _this = this;
    app._post('api/member/get_banner_info', {banner_id:index_bg}, function(result) {
      wx.setStorageSync('index_bg_img',result.data.img_url);
      _this.setData({
        index_bg_img:result.data.img_url
      })
    }); 
  },

  //日期选择
  bindDateChange: function(e) {
    let _this = this,
      date = e.detail.value,
      year = date.substr(0,4),
      month = date.substr(5,2);
    _this.setData({
      year: year,
      month: month
    })
    _this.get_bill_list();
  },


  //获取当前月份记录
  get_bill_list:function(){
    let _this = this,
      is_login = _this.data.is_login,
      year = _this.data.year,
      month = _this.data.month;
      if(is_login){
        //说明有登陆
        app._post('api/member/getBillList', {year:year,month:month}, function(result) {
          _this.setData({
            list:result.data.list,
            rece_sum:result.data.rece_sum,
            pay_sum:result.data.pay_sum,
          })
        });
      }
  },

  //前往详情
  to_detail:function(e){
    let _this = this,
      bill_id = e.currentTarget.dataset.id;;
    wx.navigateTo({
      url: './bill_detail?bill_id='+bill_id
    })
  },

  //前往登陆
  to_login:function(){
    wx.navigateTo({
      url: '../auth/auth'
    })
  },

  onShareAppMessage() {
    return {
      title: '取之有度,用之有节,则常足。',
      path: '/pages/index/index'
    }
  },
})